package cn.xing.managementSystem.interceptor;

import cn.xing.managementSystem.commons.Constants;
import cn.xing.managementSystem.domain.SysUser;
import cn.xing.managementSystem.utils.Preconditions;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author cuixing
 * @package cn.xing.managementSystem.interceptor
 * @class AuthenticationInterceptor
 * @email xing.cui@magicwindow.cn
 * @date 2018/4/21 下午4:35
 * @description
 */
@Configuration
public class AuthenticationInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
        SysUser sysUser = (SysUser)httpServletRequest.getSession().getAttribute(Constants.GLOBAL_USER_KEY);
        if(Preconditions.isBlank(sysUser)){
            httpServletResponse.sendRedirect("/management_system/login");
            return false;
        }
        Object menuName = httpServletRequest.getParameter("menu");
        if(Preconditions.isNotBlank(menuName)){
            httpServletRequest.getSession().setAttribute("menu_name", menuName);
        }
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {

    }
}